Imports System.Text
Imports System.Data
Namespace Web
  ''' <summary>
  ''' Web helper class.
  ''' </summary>
  ''' <remarks>Many of the methods use the Microsoft.Security.Application.AntiXss library.</remarks>
  Public NotInheritable Class WebHelper

    ''' <summary>
    ''' Downloads a web page.
    ''' </summary>
    ''' <param name="address">The URL of the page to download.</param>
    ''' <returns>Web page as <seealso cref="String"/></returns>
    Public Shared Function DownloadWebpage(ByVal address As Uri) As String

      If IsNothing(address) Then
        Throw New ArgumentNullException("address")
      End If

      Using request As New System.Net.WebClient()
        Dim requestData As Byte()

        'Download the data
        requestData = request.DownloadData(address)

        'Return the data by encoding it back to text!
        Return Encoding.ASCII.GetString(requestData)

      End Using

    End Function

    ''' <summary>
    ''' Downloads a web page.
    ''' </summary>
    ''' <param name="address">The URL of the page to download.</param>
    ''' <returns>Web page as <seealso cref="String"/></returns>
    Public Shared Function DownloadWebpage(ByVal address As String) As String

      If String.IsNullOrEmpty(address) Then
        Throw New ArgumentException("address")
      End If

      Return DownloadWebpage(New Uri(address))

    End Function


    ''' <summary>
    ''' HTML encodes the data in a DataTable.
    ''' </summary>
    ''' <param name="table">The DataTable to encode.</param>
    ''' <returns>Cloned <seealso cref="DataTable"/> with it's data encoded.</returns>
    Public Shared Function HtmlEncode(ByVal table As DataTable) As DataTable

      If IsNothing(table) Then
        Throw New ArgumentNullException("table")
      End If

      Dim tempTable As DataTable = table.Clone
      Dim columnCount As Int32
      Dim columnCounter As Int32


      'Get the Column count
      columnCount = tempTable.Columns.Count()

      For Each tempRow As DataRow In tempTable.Rows
        tempRow.BeginEdit()
        For columnCounter = 0 To columnCount - 1
          If tempTable.Columns(columnCounter).DataType Is System.Type.GetType("System.String") Then
            If Not IsDBNull(tempRow.Item(columnCounter)) Then
              tempRow.Item(columnCounter) = HtmlEncode(CStr(tempRow.Item(columnCounter)))
            End If
          End If
        Next
        tempRow.EndEdit()
      Next

      Return tempTable

    End Function


    ''' <summary>
    ''' HTML encodes the data in a DataSet.
    ''' </summary>
    ''' <param name="data">The DataSet to encode.</param>
    ''' <returns>Cloned <seealso cref="DataSet"/> with it's data encoded.</returns>
    Public Shared Function HtmlEncode(ByVal data As DataSet) As DataSet

      If IsNothing(data) Then
        Throw New ArgumentNullException("data")
      End If

      Dim tempDataSet As DataSet = data.Clone

      For Each tempTable As DataTable In tempDataSet.Tables
        HtmlEncode(tempTable)
      Next

      Return tempDataSet

    End Function

    ''' <summary>
    ''' HTML encodes text.
    ''' </summary>
    ''' <param name="input">The text to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function HtmlEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeHtml(input)

    End Function


    ''' <summary>
    ''' HTML decodes text.
    ''' </summary>
    ''' <param name="input">The HTML to decode.</param>
    ''' <returns>Decoded text as <seealso cref="String"/></returns>
    Public Shared Function HtmlDecode(ByVal input As String) As String

      Return System.Web.HttpUtility.HtmlDecode(input)

    End Function


    ''' <summary>
    ''' HTML encodes an text for an attribute.
    ''' </summary>
    ''' <param name="input">The text to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function HtmlAttributeEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeHtmlAttribute(input)

    End Function


    ''' <summary>
    ''' URL encodes text.
    ''' </summary>
    ''' <param name="input">The URL text to encode.</param>
    ''' <returns>Encoded URL as <seealso cref="String"/></returns>
    Public Shared Function UrlEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeUrl(input)

    End Function

    ''' <summary>
    ''' URL decodes text.
    ''' </summary>
    ''' <param name="input">The URL text to decode.</param>
    ''' <returns>Decoded text as <seealso cref="String"/></returns>
    Public Shared Function UrlDecode(ByVal input As String) As String

      Return System.Web.HttpUtility.UrlDecode(input)

    End Function

    ''' <summary>
    ''' Java Script encodes text.
    ''' </summary>
    ''' <param name="input">The text to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function JavaScriptEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeJs(input)

    End Function


    ''' <summary>
    ''' Visual Basic Script encodes text.
    ''' </summary>
    ''' <param name="input">The text to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function VisualBasicScriptEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeVbs(input)

    End Function


    ''' <summary>
    ''' XML attribute encodes text.
    ''' </summary>
    ''' <param name="input">The text to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function XmlAttributeEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeHtmlAttribute(input)

    End Function


    ''' <summary>
    ''' XML encodes text.
    ''' </summary>
    ''' <param name="input">The string to encode.</param>
    ''' <returns>Encoded text as <seealso cref="String"/></returns>
    Public Shared Function XmlEncode(ByVal input As String) As String

      Return Security.SecurityHelper.EncodeHtml(input)

    End Function


    Private Sub New()
    End Sub

  End Class
End Namespace